<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *客户列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'客户列表'),
);
$this->model('model')->checkrole(5);
$role_id=$_SESSION['role_id']==5?$_SESSION['role_id']:5;
$workerids=$this->model('model')->getworkerids($role_id);
$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
$return['workers']=$this->getall('worker',array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
//客户列表
if($_RQ['op']=='list'){
	$this->model('member')->up_haidata();
	$return['params']=array();
	$pcd=pdo_getcolumn('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['storeid']),'pcd');
	$pcd=json_decode($pcd,true);
	$city=$pcd['city'];
	$storeids=pdo_getall('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'pcd like'=>'%"'.$city.'%'),array('id'),'id');
	if($storeids){
		$storeids=array_keys($storeids);
	}else{
		$storeids=$_SESSION['storeid'];
	}
	$params=array('storeid'=>$storeids,'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['jibie_type'] && $_RQ['jibie_type']!='all'){
		$params['jibie_type']=intval($_RQ['jibie_type']);
	}
	$return['params']['jibie_type']=$_RQ['jibie_type']?:'all';
	$params['status']=array(100,110);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if(!empty($_RQ['storeid'])){
		$params['storeid']=$_RQ['storeid'];
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=$_RQ['status'];
		$return['params']['status']=$_RQ['status'];
	}
	$batch=array(
		array('type'=>'post','url'=>'jy_yxw/role5.haidata/get/','title'=>'一键领取客户'),
	);
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:100;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$list=pdo_getslice('jy_yxw_member',$params,array($pindex,$psize),$total,$fields,'','up_time desc, id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('member')->member_show($item['id']);
			if($item['jibie_type']==5 && $item['status']!=-1){
				pdo_update('jy_yxw_member',array('status'=>-1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
			}
			if($item['from_type']==1){
				$item['from_typetitle']=$this->member_from1_type[$item['from1_type']];
			}elseif($item['from_type']==2){
				$item['from_typetitle']=$this->member_from2_type[$item['from2_type']];
			}else{
				$item['from_typetitle']=$this->member_from_type[$item['from_type']];
			}
			$geninfo=pdo_get('jy_yxw_genjin_log',array('uniacid'=>$_SESSION['uniacid'],'memberid'=>$item['id']),array('remark','workerid'),'id desc');
			$item['workertitle']=pdo_getcolumn('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$geninfo['workerid']),'title');
			$item['workertitle']=$item['workertitle']?:'员工';
			$geninfo['remark']=$geninfo['remark']?:'无内容';
			$item['geninfo']='['.$item['workertitle'].']:'.$geninfo['remark'];
		}
	}
	$tip='[温馨提示]：</br>1、这里只显示公海的客户，您可以自由领取！公海范围：以城市为单位，例如广州5家门店， 那么公海就是5家互通';
	$basic=$this->plugin_setting('basic');
	$tip .='</br>2、'.$basic['sf_day'].'天内，属于【跟进中】状态的客户，超时没跟进的才会进入公海';
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		//'export'=>'export',
		'list'=>array('data'=>'list','tip'=>$tip,'del_message'=>'确认删除该客户吗？','total'=>$total,'psize'=>$psize,'del_op'=>'del','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'storeid','title'=>'所属门店','type'=>'select2','options'=>'stores'),
			array('name'=>'jibie_type','title'=>'客户级别','type'=>'radio2','options'=>'jibie_type'),
			array('name'=>'from_typetitle','title'=>'渠道','type'=>'string'),
			array('name'=>'geninfo','title'=>'最近跟进记录','type'=>'string'),
			//array('name'=>'worker3id','title'=>'网络专员','type'=>'select2','options'=>'workers'),
			array('name'=>'worker4id','title'=>'前台员','type'=>'select2','options'=>'workers'),
			array('name'=>'worker5id','title'=>'电客员','type'=>'select2','options'=>'workers'),
			array('name'=>'worker6id','title'=>'销售员员','type'=>'select2','options'=>'workers'),
			array('name'=>'worker7id','title'=>'体验员','type'=>'select2','options'=>'workers'),
			array('name'=>'worker8id','title'=>'试用员','type'=>'select2','options'=>'workers'),
			array('name'=>'status','title'=>'状态','type'=>'radio2','options'=>'status'),
			array('name'=>'up_time','title'=>'最近跟进时间','type'=>'time'),
			array('name'=>'createtime','title'=>'添加日期','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','action'=>'jy_yxw/role5.haidata/get/','title'=>'领取客户'),
			array('type'=>'modal','action'=>'/member.genjin_log/onelist/','title'=>'查看跟进记录'),
		),'batch'=>$batch),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'手机号','name'=>'mobile','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'客户名称','name'=>'title','help'=>''),
			array('type'=>'radio','width'=>'15','title'=>'公海类别','name'=>'status','options'=>'haistatus'),
			array('name'=>'storeid','width'=>'15','title'=>'所属门店','type'=>'select','options'=>'stores'),
		),
	);
	$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid'],'pcd like'=>'%"'.$city.'%'),array('id','title'),'id');
	$return['status']=$this->member_status;
	$return['haistatus']=array(100=>'销售公海中',110=>'电销公海中');
	$return['status']['all']='全部';
	$return['jibie_type']=$this->member_jibie_type;
	$return['from_type']=$this->member_from_type;
	$return['from1_type']=$this->member_from1_type;
	$return['from2_type']=$this->member_from2_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='get'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择客户','error');
	}
	$update=array('worker5id'=>$_SESSION['workerid'],'storeid'=>$_SESSION['storeid'],'status'=>1,'up_time'=>time());
	foreach($ids as $id){
		$member=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		if($_SESSION['storeid']!=$member['storeid']){
			$update['worker3id']=$update['worker4id']=0;
		}
		if($member['status']!=100 && $member['status']!=110){
			exi($member['title'].'-已被其他人领取！','error');
		}
		$uplog=array(
					'memberid'=>$id,
					'remark'=>'公海领取成功',
					'status'=>1,
					'type'=>2,
		);
		$this->model('uplog')->addgenjin_log($uplog);
		pdo_update('jy_yxw_member',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}
	
	exi('领取成功！');
}
